cmake_minimum_required(VERSION 3.0)

project(grid_db LANGUAGES C Fortran)

if (CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
  set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -ffree-line-length-none -ffpe-trap=invalid,zero,overflow")
  if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -O0 -fcheck=all -g -fbacktrace")
  else ()
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Ofast")
  endif ()
  # FIXME: OpenMP causes sigmentation faults.
  # set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fopenmp")
elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "Intel")
  if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -no-wrap-margin -O0 -check bounds -g -traceback")
  else ()
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -no-wrap-margin -O3")
  endif ()
endif ()

include(FindPkgConfig)

# ------------------------------------------------------------------------------
#                                    NETCDF
if (DEFINED ENV{NETCDF} AND (NOT DEFINED ENV{NETCDF_ROOT}))
  set(ENV{NETCDF_ROOT} $ENV{NETCDF})
endif ()
if (DEFINED ENV{NETCDF_ROOT})
  include_directories("$ENV{NETCDF_ROOT}/include")
  link_directories("$ENV{NETCDF_ROOT}/lib")
else ()
  find_package(PkgConfig)
  if (PKG_CONFIG_FOUND)
    pkg_search_module(NETCDF netcdf)
    if (NETCDF_FOUND)
      include_directories(${NETCDF_INCLUDEDIR})
      link_directories(${NETCDF_LIBDIR})
    else ()
      message(FATAL_ERROR "Pkg-config could not find netcdf library!")
    endif ()
  else ()
    message(FATAL_ERROR "Unable to find pkg-config library!")
  endif ()
endif ()

# ------------------------------------------------------------------------------
#                                  MONGO
pkg_check_modules(MONGOC libmongoc-1.0)

if (MONGOC_FOUND)
  include_directories(${MONGOC_INCLUDE_DIRS})
  link_directories(${MONGOC_LIBRARY_DIRS})
else ()
  message(FATAL_ERROR "No MONGOC is found! Build without it.")
endif ()

add_subdirectory(lib/container)
add_subdirectory(lib/string)
add_subdirectory(lib/flogger)

set(sources
  src/mongoc_const.h
  src/mongoc_data.h
  src/mongoc_common.c
  src/mongof_common_mod.F90
  src/utils_mod.F90
)

add_executable(write_tp_10km_grid.exe ${sources} src/write_tp_2km_grid.c src/write_tp_10km_grid.F90)
target_link_libraries(write_tp_10km_grid.exe netcdff ${MONGOC_LIBRARIES} fortran_string flogger)

add_executable(write_tp_10km_climate.exe ${sources} src/write_tp_2km_yearly.c src/write_tp_10km_climate.F90)
target_link_libraries(write_tp_10km_climate.exe netcdff ${MONGOC_LIBRARIES} fortran_string flogger)

add_executable(write_tp_2km_grid.exe ${sources} src/write_tp_2km_grid.c src/write_tp_2km_grid.F90)
target_link_libraries(write_tp_2km_grid.exe netcdff ${MONGOC_LIBRARIES} fortran_string flogger)

add_executable(write_tp_2km_config.exe ${sources} src/write_tp_2km_config.c src/write_tp_2km_config.F90)
target_link_libraries(write_tp_2km_config.exe netcdff ${MONGOC_LIBRARIES} fortran_string flogger)

add_executable(write_tp_2km_yearly.exe ${sources} src/write_tp_2km_yearly.c src/write_tp_2km_yearly.F90)
target_link_libraries(write_tp_2km_yearly.exe netcdff ${MONGOC_LIBRARIES} fortran_string flogger)

add_executable(write_tp_2km_monthly.exe ${sources} src/write_tp_2km_yearly.c src/write_tp_2km_monthly.F90)
target_link_libraries(write_tp_2km_monthly.exe netcdff ${MONGOC_LIBRARIES} fortran_string flogger)

add_executable(write_tp_2km_ymonthly.exe ${sources} src/write_tp_2km_ymonthly.c src/write_tp_2km_ymonthly.F90)
target_link_libraries(write_tp_2km_ymonthly.exe netcdff ${MONGOC_LIBRARIES} fortran_string flogger)

add_executable(write_tp_2km_annual_mean.exe ${sources} src/write_tp_2km_yearly.c src/write_tp_2km_annual_mean.F90)
target_link_libraries(write_tp_2km_annual_mean.exe netcdff ${MONGOC_LIBRARIES} fortran_string flogger)
